package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.entity.Product;
import com.luck.easyfoodbatch.mvp.mine.EditGoods2Model;
import com.luck.easyfoodbatch.mvp.mine.EditGoods2Presenter;
import com.luck.easyfoodbatch.mvp.mine.EditGoods2View;
import com.luck.easyfoodbatch.mvp.mine.GoodsDetailModel;
import com.luck.easyfoodbatch.mvp.mine.GoodsDetailPresenter;
import com.luck.easyfoodbatch.mvp.mine.GoodsDetailView;
import com.luck.easyfoodbatch.mvp.model.EditGoods;
import com.luck.easyfoodbatch.mvp.model.GoodsDetail;
import com.luck.easyfoodbatch.pickerview.PopBirthHelper;
import com.luck.easyfoodbatch.pickerview.PopTimeBirthHelper;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UploadParameterActivity extends BaseActivity implements GoodsDetailView, EditGoods2View {

    @BindView(R.id.btn_next_par)
    Button btn_next_par;
    @BindView(R.id.et_uname)
    EditText etUname;
    @BindView(R.id.et_save_way)
    EditText etSaveWay;
    @BindView(R.id.et_expried_time)
    EditText et_expried_time;
    @BindView(R.id.tv_pd)
    TextView tvPd;
    @BindView(R.id.et_food_additives)
    EditText etFoodAdditives;
    @BindView(R.id.et_net_content)
    EditText etNetContent;
    @BindView(R.id.et_pack_way)
    EditText et_pack_way;
    @BindView(R.id.et_taste)
    EditText etTaste;
    @BindView(R.id.et_brand)
    EditText et_brand;
    @BindView(R.id.et_product_name)
    EditText etProductName;
    @BindView(R.id.et_series)
    EditText etSeries;
    @BindView(R.id.et_bar_code)
    EditText etBarCode;
    @BindView(R.id.et_factory_name)
    EditText etFactoryName;
    @BindView(R.id.et_factory_address)
    EditText etFactoryAddress;
    @BindView(R.id.et_factory_phone)
    EditText etFactoryPhone;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    private String gid = "";
    private PopTimeBirthHelper birthHelper;
    private int type = 0;
    private int flag = 0;
    private GoodsDetailModel detailModel;
    private EditGoods2Model goods2Model;
    private int product_type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        flag = getIntent().getIntExtra("type", 0);
        String title = "";
        if (flag == 1) {
            // 编辑状态
            title = "编辑商品(商品参数)";
        } else {
            title = "上传商品(商品参数)";
        }
        setContentView(R.layout.activity_upload_parameter, title, MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        gid = getIntent().getStringExtra("gid");
        product_type = getIntent().getIntExtra("product_type", 0);
        if (flag == 1) {
            btn_next_par.setText("保存");
            showProgressDialog(getString(R.string.app_loading));
            detailModel = new GoodsDetailPresenter(this, mContext);
            detailModel.requestGoodsDetail(gid);
        } else {
            btn_next_par.setText("下一步");
        }
        birthHelper = new PopTimeBirthHelper(this);
        birthHelper.setOnClickOkListener(new PopTimeBirthHelper.OnClickOkListener() {
            @Override
            public void onClickOk(String birthday) {
                tvPd.setText(birthday);
            }
        });
    }

    @OnClick({R.id.btn_next_par, R.id.tv_pd})
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.tv_pd:
                // 生产日期
                type = 1;
                birthHelper.show(v);
                break;
            case R.id.btn_next_par:
                String burden_sheet = etUname.getText().toString().trim();
                String save_way = etSaveWay.getText().toString().trim();
                String expried_time = et_expried_time.getText().toString().trim();
                String pd = tvPd.getText().toString().trim();
                String food_additives = etFoodAdditives.getText().toString().trim();
                String net_content = etNetContent.getText().toString().trim();
                String pack_way = et_pack_way.getText().toString().trim();
                String taste = etTaste.getText().toString().trim();
                String product_name = etProductName.getText().toString().trim();
                String brand = et_brand.getText().toString().trim();
                String series = etSeries.getText().toString().trim();
                String bar_code = etBarCode.getText().toString().trim();
                String factory_name = etFactoryName.getText().toString().trim();
                String factory_address = etFactoryAddress.getText().toString().trim();
                String factory_phone = etFactoryPhone.getText().toString().trim();
                if (Tools.isNull(burden_sheet)) {
                    //T.showToast(mContext, "请输入配料表");
                    burden_sheet = "";
                } else if (Tools.isNull(save_way)) {
                    //T.showToast(mContext, "请输入储存方式");
                    save_way = "";
                } else if (Tools.isNull(expried_time)) {
                    //T.showToast(mContext, "请输入保质期天数");
                    expried_time = "";
                } else if (Tools.isNull(pd)) {
                    //T.showToast(mContext, "请选择生产日期");
                    pd = "";
                } else if (Tools.isNull(food_additives)) {
                    //T.showToast(mContext, "请输入食品添加剂");
                    food_additives = "";
                } else if (Tools.isNull(net_content)) {
                    //T.showToast(mContext, "请输入净含量");
                    net_content = "";
                } else if (Tools.isNull(pack_way)) {
                    //T.showToast(mContext, "请输入包装方式");
                    pack_way = "";
                } else if (Tools.isNull(taste)) {
                    //T.showToast(mContext, "请输入口味");
                    taste = "";
                } else if (Tools.isNull(product_name)) {
                    //T.showToast(mContext, "请输入产品名称");
                    product_name = "";
                } else if (Tools.isNull(brand)) {
                    // T.showToast(mContext, "请输入品牌");
                    brand = "";
                } else if (Tools.isNull(series)) {
                    //T.showToast(mContext, "请输入商品系列");
                    series = "";
                } else if (Tools.isNull(bar_code)) {
                    // T.showToast(mContext, "请输入商品条形码");
                    bar_code = "";
                } else if (Tools.isNull(factory_name)) {
                    //T.showToast(mContext, "请输入工厂名");
                    factory_name = "";
                } else if (Tools.isNull(factory_address)) {
                    //T.showToast(mContext, "请输入工厂详细地址");
                    factory_address = "";
                } else if (Tools.isNull(factory_phone)) {
                    //T.showToast(mContext, "请输入厂家联系方式");
                    factory_phone = "";
                }
                Product product = new Product();
                product.setBurden_sheet(burden_sheet);
                product.setSave_way(save_way);
                product.setPd("");
                product.setExpried_time(expried_time);
                product.setFood_additives(food_additives);
                product.setNet_content(net_content);
                product.setPack_way(pack_way);
                product.setTaste(taste);
                product.setProduct_name(product_name);
                product.setBrand(brand);
                product.setSeries(series);
                product.setGid(gid);
                product.setBar_code(bar_code);
                product.setFactory_name(factory_name);
                product.setFactory_address(factory_address);
                product.setFactory_phone(factory_phone);
                bundle.putSerializable("product", product);
                bundle.putInt("product_type", product_type);
                intent.putExtras(bundle);
                if (flag == 1) {
                    // 编辑状态
                    showProgressDialog(getString(R.string.app_loading));
                    goods2Model = new EditGoods2Presenter(this, mContext);
                    goods2Model.requestEditGoods(0, product);
                } else {
                    intent.setClass(mContext, UploadGraphicDetailsActivity.class);
                    startActivityForResult(intent, 200);
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        }
    }

    @Override
    public void getGoodsDetailSuccess(GoodsDetail model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                GoodsDetail.DataBean modelData = model.getData();
                if (modelData != null) {
                    etUname.setText(modelData.getBurden_sheet());
                    etSaveWay.setText(modelData.getSave_way());
                    et_expried_time.setText(modelData.getExpried_time() + "");
                    if (!Tools.isNull(modelData.getPd())) {
                        tvPd.setText(Tools.SubTime2(modelData.getPd()));
                    } else {
                        tvPd.setText("");
                    }
                    etFoodAdditives.setText(modelData.getFood_additives());
                    etNetContent.setText(modelData.getNet_content());
                    et_pack_way.setText(modelData.getPack_way());
                    etTaste.setText(modelData.getTaste());
                    etProductName.setText(modelData.getProduct_name());
                    et_brand.setText(modelData.getBrand());
                    etSeries.setText(modelData.getSeries());
                    etBarCode.setText(modelData.getBar_code());
                    etFactoryName.setText(modelData.getFactory_name());
                    etFactoryAddress.setText(modelData.getFactory_address());
                    etFactoryPhone.setText(modelData.getFactory_phone());
                }
            }
        }
    }

    @Override
    public void editGoods2Success(EditGoods model) {
        if (model != null && model.getStatus() == HttpUtil.RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
